package com.alkaid.anime.service.impl;

import com.alkaid.anime.service.HistoryService;
import com.alkaid.anime.utils.RedisUtils;
import com.alkaid.commons.core.CacheKeyDefinition;
import com.alkaid.commons.param.anime.AnimeHistoryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Kylin
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    private RedisUtils redisUtils;

    @Autowired
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    @Override
    public void setHistory(Long userId, AnimeHistoryParam select) {
        String key1 = CacheKeyDefinition.getAnimeHistoryKey(userId, select.getAnimeId());
        redisUtils.save2Redis(key1, select, CacheKeyDefinition.CACHE_TIME_WEEK);
        String key2 = CacheKeyDefinition.getAnimeVideoHistoryKey(userId, select.getAnimeVideoId());
        redisUtils.save2Redis(key2, select, CacheKeyDefinition.CACHE_TIME_WEEK);
    }

    @Override
    public AnimeHistoryParam getHistoryByAnimeId(Long userId, Integer animeId) {
        if (animeId == null) {
            return null;
        }
        String key = CacheKeyDefinition.getAnimeHistoryKey(userId, animeId);
        return (AnimeHistoryParam) redisUtils.getFromRedis(key);
    }

    @Override
    public AnimeHistoryParam getHistoryByAnimeVideoId(Long userId, Long animeVideoId) {
        if (animeVideoId == null) {
            return null;
        }
        String key = CacheKeyDefinition.getAnimeVideoHistoryKey(userId, animeVideoId);
        return (AnimeHistoryParam) redisUtils.getFromRedis(key);
    }
}
